//
//  ARCViewController.swift
//  SwiftTest
//
//  Created by midea on 16/3/17.
//  Copyright © 2016年 Vincent. All rights reserved.
//

import Foundation

private class Person {
    let name : String
    init(name:String){
        self.name = name
        print(name + " is being initialized")
    }
    deinit{
        print(name + " is being deinited")
    }
}

private class Dog {
    fileprivate var line : Line?
    deinit{
        print("Can Dog deinit?")
    }
}

private class Line {
    fileprivate var dog : Dog?
    deinit{
        print("Can Line deinit?")
    }
}

private class Cup {
    fileprivate var water : Water?
    deinit{
        print("Can Cup deinit?")
    }
}

private class Water {
    fileprivate weak var cup : Cup?
    deinit{
        print("Can Water deinit?")
    }
}

private class Customer {
    fileprivate var creditCard : CreditCard?
    deinit{
        print("Can Customer deinit?")
    }
}

private class CreditCard {
    fileprivate unowned let customer : Customer
    init(customer:Customer){
        self.customer = customer
    }
    deinit{
        print("Can CreditCard deinit?")
    }
}

private class HTMLElement {
    let name: String
    let text: String?
    lazy var asHTML: () -> String = {
        if let text = self.text {
            return "<\(self.name)>\(text)"
        } else {
            return "<\(self.name) />"
        }
    }
    init(name: String, text: String? = nil) {
        self.name = name
        self.text = text
    }
    deinit {
        print("\(name) is being deinitialized")
    }
}

class ARCViewController : BaseViewController {
    
    fileprivate var reference1 : Person?
    fileprivate var reference2 : Person?
    fileprivate var reference3 : Person?
    
    fileprivate var timer: Timer?
    fileprivate var interval : Int = 0
    
    fileprivate var dog : Dog?
    fileprivate var line : Line?
    
    fileprivate var cup : Cup?
    fileprivate var water : Water?
    
    fileprivate var customer : Customer?
    fileprivate var creditCard : CreditCard?
    
    fileprivate var paragraph: HTMLElement? = HTMLElement(name: "p", text: "hello, world")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        reference1 = Person.init(name: "Lucy")
        reference2 = reference1
        reference3 = reference2
        
//        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerRunning"), userInfo:nil, repeats: true)
        
        //====== error reference ========
        dog = Dog()
        line = Line()
        dog?.line = line
        line?.dog = dog
        //====== error reference ========
        
        //====== weak reference ========
        cup = Cup()
        water = Water()
        cup?.water = water
        water?.cup = cup
        //====== weak reference ========
        
        //====== unowned reference ========
        customer = Customer()
        creditCard = CreditCard.init(customer: customer!)
        customer?.creditCard = creditCard
        //====== unowned reference ========
        
    }
    
    deinit{
        timer?.invalidate()
        print("ARCViewController's deinit")
    }
    
    func timerRunning(){
        switch self.interval{
        case 5:
            reference1 = nil
            print("interval is " + String(self.interval) + " and reference1 is " + String(describing: reference1))
        case 10:
            reference2 = nil
            print("interval is " + String(self.interval) + " and reference2 is " + String(describing: reference2))
        case 15:
            reference3 = nil
            print("interval is " + String(self.interval) + " and reference3 is " + String(describing: reference3))
        default:
            print("timer is running")
        }
        
        if self.interval >= 15 {
            timer?.invalidate()
        }
        self.interval += 1
    }
}
